package cn.yunhe.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;

import cn.yunhe.model.Area;
import cn.yunhe.model.House;
import cn.yunhe.model.Street;
import cn.yunhe.model.Type;
import cn.yunhe.model.User;
import cn.yunhe.services.HouseServices;
import cn.yunhe.util.BaseAction;

/**
 * 
 * @author Administrator
 *
 */
public class XiuGaiAction extends BaseAction {
	HouseServices hs = new HouseServices();
	private String houseid;
	private House house;
	private String userid;
	private String typeid;
	private String streetid;
	private String areaid;
	/* 上传照片 */
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;

	public String editHouse() throws Exception {
		Area area = new Area();
		area.setAreaid(Integer.parseInt(areaid));
		Street street = new Street();
		street.setStreetid(Integer.parseInt(streetid));
		street.setArea(area);
		Type type = new Type();
		type.setTypeid(Integer.parseInt(typeid));
		User user = new User();
		user.setUserid(Integer.parseInt(userid));
		House h = new House(Integer.parseInt(houseid), user, type, street, house.getTitle(), house.getContent(),
				uploadFileName, house.getPrice(), house.getPubdate(), house.getFloorage(), house.getContact());
		hs.editHouse(h);
		String uploadPath = "C://Users//Administrator//git//ZuFangXiTong//src//main//webapp//" + getSavePath() + "/"
				+ uploadFileName;
		doUpload(upload, uploadPath);
		return SUCCESS;
	}
	
	public String queryHouse(){
		if(!"".equals(houseid)&&null!=houseid){
			house = hs.getHouseById(Integer.parseInt(houseid));
			return SUCCESS;
		}
		return ERROR;
	}
	
	public void doUpload(File file, String uploadFilePath) throws Exception {
		System.out.println(uploadFilePath);
		FileOutputStream fos = new FileOutputStream(uploadFilePath);
		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) != -1) {
			fos.write(buffer, 0, len);
		}
		fis.close();
		fos.close();
	}

	public House getHouse() {
		return house;
	}

	public void setHouse(House house) {
		this.house = house;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getTypeid() {
		return typeid;
	}

	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}

	public String getStreetid() {
		return streetid;
	}

	public void setStreetid(String streetid) {
		this.streetid = streetid;
	}

	public String getAreaid() {
		return areaid;
	}

	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getHouseid() {
		return houseid;
	}

	public void setHouseid(String houseid) {
		this.houseid = houseid;
	}

	public HouseServices getHs() {
		return hs;
	}

	public void setHs(HouseServices hs) {
		this.hs = hs;
	}
	
}
